package com.TanNgee.ThreadPoolExercise;

/**
 * @author 陈毅
 * @create 2022/4/26 0:16
 * 模拟取款业务
 */
public class MyTask implements Runnable {
    private static int balance = 1000;
    private String name;

    public MyTask(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        synchronized (MyTask.class) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (balance >= 200) {
                final String name = Thread.currentThread().getName();
                balance -= 200;
                System.out.println("线程" + name + "取款成功，当前余额为" + balance);
            } else {
                final String name = Thread.currentThread().getName();
                System.out.println("线程" + name + "取款失败，当前余额为" + balance);

            }
        }
    }
}
